package com.zhkj.txg.module.product.ui

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.ScreenUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.product.adapter.RankAdapter
import com.zhkj.txg.module.product.vm.RankViewModel
import kotlinx.android.synthetic.main.activity_rank.*
import kotlin.math.abs

class ProductRankActivity : ViewModelActivity<RankViewModel>() {

    companion object {
        private val CAT_ID = "catId"

        fun open(activity: Activity, catId: Long) {
            val intent = Intent(activity, ProductRankActivity::class.java)
                .putExtra(CAT_ID, catId)
            activity.startActivity(intent)
        }
    }

    override fun setViewModel(): RankViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleRank
    override val layoutResId: Int = R.layout.activity_rank

    private val rankAdapter = RankAdapter().apply {
        headerWithEmptyEnable = true
        loadMoreModule?.setOnLoadMoreListener {
            viewModel.rankProduct()
        }
    }

    override fun initUI() {
        with(rvRank) {
            layoutManager = LinearLayoutManager(this@ProductRankActivity)
            adapter = rankAdapter
            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                val llm = layoutManager as LinearLayoutManager
                val pos = IntArray(2)
                val maxScrollHeight =
                    ScreenUtil.dip(this@ProductRankActivity, 100)
                val originHeight = ScreenUtil.dip(this@ProductRankActivity, 50)

                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    // 设置title的透明度
                    if (llm.findFirstVisibleItemPosition() == 0 && llm.getChildAt(0) != null) {
                        llm.getChildAt(0)!!.getLocationInWindow(pos)
                            val originY = abs(pos[1] - originHeight)
                            if (originY <= maxScrollHeight) {
                                val alpha = (originY.toFloat() / maxScrollHeight * 255).toInt()
                                titleRank.setBackgroundColor(Color.argb(alpha, 255, 255, 255))
                            } else {
                                titleRank.setBackgroundColor(Color.argb(255, 255, 255, 255))
                            }
                    }
                }
            })
        }
        with(rankAdapter) {
            setEmptyView(R.layout.layout_empty_data)
            addHeaderView(layoutInflater.inflate(R.layout.item_rank_top, null))
        }
    }

    override fun initData() {
        viewModel.catId = intent.getLongExtra(CAT_ID, 0)
        viewModel.rankProduct()
    }

    override fun initViewModel() {
        viewModel.rankProductLiveData.observe(this, Observer { response ->
            when (response.status) {
                HttpResponseStatus.Status.START -> Unit
                HttpResponseStatus.Status.SUCCESS -> response.response?.let {
                    // 数据填充
                    if (it.loadMore) rankAdapter.addData(it.data)
                    else rankAdapter.setNewData(it.data)
                    // 判断加载状态
                    if (it.hasMore) rankAdapter.loadMoreModule?.loadMoreComplete()
                    else rankAdapter.loadMoreModule?.loadMoreEnd(false)
                }
                HttpResponseStatus.Status.FAILURE -> {
                    response.exception?.printStackTrace()
                    toast(response.statusTip)
                    rankAdapter.loadMoreModule?.loadMoreFail()
                }
            }
        })
    }
}